#include "buzzer_control.h"

void buzzer_init(void)
{
  gpio_init_type gpio_init_struct;
  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE);
  gpio_default_para_init(&gpio_init_struct);

  gpio_init_struct.gpio_pins = GPIO_PINS_2;
  gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init(GPIOA, &gpio_init_struct);

  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE2, GPIO_MUX_1);

  tmr_output_config_type tmr_oc_init_structure;
  tmr_base_init(TMR2, 8999, 127);
  tmr_cnt_dir_set(TMR2, TMR_COUNT_UP);
  tmr_clock_source_div_set(TMR2, TMR_CLOCK_DIV1);

  tmr_output_default_para_init(&tmr_oc_init_structure);
  tmr_oc_init_structure.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;
  tmr_oc_init_structure.oc_idle_state = FALSE;
  tmr_oc_init_structure.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
  tmr_oc_init_structure.oc_output_state = TRUE;
  tmr_output_channel_config(TMR2, TMR_SELECT_CHANNEL_3, &tmr_oc_init_structure);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);

  /* tmr enable counter */
  tmr_counter_enable(TMR2, TRUE);
}

void buzzer_do(void)
{
  tmr_period_value_set(TMR2, 6488);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, tmr_period_value_get(TMR2) / 2);
  vTaskDelay(pdMS_TO_TICKS(200));
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
}

void buzzer_re(void)
{
  tmr_period_value_set(TMR2, 5102);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, tmr_period_value_get(TMR2) / 2);
  vTaskDelay(pdMS_TO_TICKS(200));
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
}

void buzzer_mi(void)
{
  tmr_period_value_set(TMR2, 4545);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, tmr_period_value_get(TMR2) / 2);
  vTaskDelay(pdMS_TO_TICKS(200));
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
}

void buzzer_fa(void)
{
  tmr_period_value_set(TMR2, 4046);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, tmr_period_value_get(TMR2) / 2);
  vTaskDelay(pdMS_TO_TICKS(200));
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
}

void buzzer_sol(void)
{
  tmr_period_value_set(TMR2, 3605);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, tmr_period_value_get(TMR2) / 2);
  vTaskDelay(pdMS_TO_TICKS(200));
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
}

void buzzer_la(void)
{
  tmr_period_value_set(TMR2, 3403);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, tmr_period_value_get(TMR2) / 2);
  vTaskDelay(pdMS_TO_TICKS(200));
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
}

void buzzer_si(void)
{
  tmr_period_value_set(TMR2, 3032);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, tmr_period_value_get(TMR2) / 2);
  vTaskDelay(pdMS_TO_TICKS(200));
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
}

void lora_do(void)
{
  char *send_msg = "DO";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}

void lora_re(void)
{
  char *send_msg = "RE";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}

void lora_mi(void)
{
  char *send_msg = "MI";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}

void lora_fa(void)
{
  char *send_msg = "FA";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}

void lora_sol(void)
{
  char *send_msg = "SOL";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}

void lora_la(void)
{
  char *send_msg = "LA";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}

void lora_si(void)
{
  char *send_msg = "SI";
  llcc68_send((uint8_t *)send_msg, strlen(send_msg));
}